package gui;


import java.util.LinkedList;
import java.util.List;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import schedule.Event;
import schedule.Schedule;

import com.thirdnf.ResourceScheduler.AbstractScheduleModel;
import com.thirdnf.ResourceScheduler.Appointment;
import com.thirdnf.ResourceScheduler.AppointmentVisitor;
import com.thirdnf.ResourceScheduler.Resource;
import com.thirdnf.ResourceScheduler.ResourceVisitor;

/**
 * Represente un modele par semaine pour un emploi du temps
 * @author Stanislas KOBAK / Celine BARILLERE
 *
 */
public class WeekModel extends AbstractScheduleModel 
{
	public List<Resource> days;
	private List<Appointment> appointments;
	
	/**
	 * Constructeur
	 * @param schedule L'emploi du temps
	 * @param dateNow La date du premier jour de la semaine visible sur l'emploi du temps
	 */
	public WeekModel(Schedule schedule, LocalDate dateNow)
	{
		days = new LinkedList<Resource>();
		days.add(Day.Monday());
		days.add(Day.Tuesday());
		days.add(Day.Wednesday());
		days.add(Day.Thursday());
		days.add(Day.Friday());
		days.add(Day.Saturday());
		days.add(Day.Sunday());
		
		LocalDate nextWeek = dateNow.plusDays(7);
		
		appointments = new LinkedList<Appointment>();
		
		for(Event event : schedule)
    	{
			LocalDate dateEvent = event.getStartDate().toLocalDate();
    		int dayOfWeek = event.getStartDate().getDayOfWeek();
			if (dateEvent.isAfter(dateNow) && dateEvent.isBefore(nextWeek) || dateEvent.equals(dateNow)) {
	    		Appointment appointment = new MyAppointment(event, days.get(dayOfWeek-1));
	    		appointments.add(appointment);
			}
    	}
	}
    
    public void visitAppointments(AppointmentVisitor appointmentVisitor, LocalDate localDate)
    {
    	for(Appointment appointment : appointments)
    	{
    		appointmentVisitor.visitAppointment(appointment);
    	}
    }

    public void visitResources(ResourceVisitor resourceVisitor, LocalDate localDate)
    {
    	for(Resource day : days)
    	{
    		resourceVisitor.visitResource(day);
    	}
    }
    
    /**
     * Definit l'heure de debut de la journee
     */
    public LocalTime getStartTime(LocalDate localDate)
    {
        return new LocalTime(8,0,0);
    }

    /**
     * Definit l'heure de fin de la journee
     */
    public LocalTime getEndTime(LocalDate localDate)
    {
        return new LocalTime(20,0,0);
    }
    
    /**
     * Ajoute un evenement a l'affichage
     * @param appointment L'evenement
     */
    public void addAppointment(Appointment appointment)
    {    	
    	appointments.add(appointment);
    	fireAppointmentAdded(appointment);
    }

    /**
     * Modifie un evenement de l'affichage
     * @param appointment L'evenement
     */
    public void updateAppointment(Appointment appointment)
    {
        fireAppointmentUpdated(appointment);
    }
    
    /**
     * Supprime un evenement de l'affichage
     * @param appointment L'evenement
     */
    public void deleteAppointment(Appointment appointment)
    {
    	appointments.remove(appointment);
        fireAppointmentRemoved(appointment);
    }
}
